package cn.flc.service.impl;

import cn.flc.bean.UserEntity;
import cn.flc.bean.dto.UserDto;
import cn.flc.jsonresult.JSONResult;
import cn.flc.mapper.UserMapper;
import cn.flc.service.UserService;
import cn.flc.util.VerifyUtils;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;


/**
 * @author: Xzh
 * @date: 2023/3/3 16:39
 * @description:
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public JSONResult register(UserDto user) {
        UserEntity userEntity = new UserEntity();
        String username = user.getUsername();
        String nickname = user.getNickname();
        String remarks = user.getRemarks();
        String userPassword = user.getUserPassword();
        //判断注册的是不是email
        if(VerifyUtils.isEmail(username)){
            userEntity.setEmail(username);
            userEntity.setNickname(nickname);
            //对密码加盐
            String salt = RandomUtil.randomString(10);
            //设置盐
            userEntity.setSalt(salt);
            userPassword=salt+userPassword;
            userPassword= SecureUtil.md5(userPassword);
            userEntity.setUserPassword(userPassword);
            userEntity.setRemarks(remarks);
            //判断用户名或者手机号是否注册,微信和手机号一般一样不做测试
            UserEntity exist = userMapper.findExist(userEntity);
            if(exist!=null){
                return JSONResult.error("您注册失败");
            }
            //注册的时候设创建时间
            Date date = new Date();
            userEntity.setCreateTime(new Timestamp(date.getTime()));
            //注册的时候设置账户状态激活
            userEntity.setTbStatus("1");
            userMapper.register(userEntity);
            return JSONResult.success("注册成功");
        }
        //判断注册的是不是手机号
        if(VerifyUtils.isMobile(username)){
            userEntity.setPhone(username);
            userEntity.setNickname(nickname);
            //对密码加盐
            String salt = RandomUtil.randomString(10);
            //设置盐
            userEntity.setSalt(salt);
            userPassword=salt+userPassword;
            userPassword= SecureUtil.md5(userPassword);
            userEntity.setUserPassword(userPassword);
            userEntity.setRemarks(remarks);
            //判断用户名或者手机号是否注册,微信和手机号一般一样不做测试
            UserEntity exist = userMapper.findExist(userEntity);
            if(exist!=null){
                return JSONResult.error("您注册失败");
            }
            //注册的时候设创建时间
            Date date = new Date();
            userEntity.setCreateTime(new Timestamp(date.getTime()));
            //注册的时候设置账户状态激活
            userEntity.setTbStatus("1");
            userMapper.register(userEntity);
            return JSONResult.success("注册成功");
        }
        //判断是不是用户名
        if(username!=null&&username!=""){
            userEntity.setUsername(username);
            userEntity.setNickname(nickname);
            //对密码加盐
            String salt = RandomUtil.randomString(10);
            //设置盐
            userEntity.setSalt(salt);
            userPassword=salt+userPassword;
            userPassword= SecureUtil.md5(userPassword);
            userEntity.setUserPassword(userPassword);
            userEntity.setRemarks(remarks);
            //判断用户名或者手机号是否注册,微信和手机号一般一样不做测试
            UserEntity exist = userMapper.findExist(userEntity);
            if(exist!=null){
                return JSONResult.error("您注册失败");
            }
            //注册的时候设创建时间
            Date date = new Date();
            userEntity.setCreateTime(new Timestamp(date.getTime()));
            //注册的时候设置账户状态激活
            userEntity.setTbStatus("1");
            userMapper.register(userEntity);
            return JSONResult.success("注册成功");
        }
        return JSONResult.success("注册成功");
    }

    @Override
    public JSONResult login(UserDto user) {
        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(user,userEntity);
        //先查询用户
        UserEntity exist = userMapper.findExist(userEntity);
        String userPassword = user.getUserPassword();
        String salt = exist.getSalt();
        userPassword=salt+userPassword;
        userPassword= SecureUtil.md5(userPassword);
        //对密码进行比对字符串不能用等等比较
        if(!userPassword.equals(exist.getUserPassword())){
            return JSONResult.error("密码不正确");
        }
        //设置更新数据
        Date date = new Date();
        userEntity.setUpdateTime(new Timestamp(date.getTime()));
        userMapper.update(userEntity);
        //深烤呗
        UserEntity login = userMapper.login(userEntity);
        if(login != null){
            return JSONResult.success("登录成功");
        }
        else {
            return JSONResult.error("登录失败");
        }
    }

    @Override
    public JSONResult remind(UserDto user) {
        String username = user.getUsername();
        UserEntity remind = userMapper.remind(user);
        String remarks = remind.getRemarks();
        return JSONResult.success(remarks);
    }
}
